package cn.hongyu.controller;

import cn.hongyu.bean.User;
import cn.hongyu.service.UserService;
import cn.hongyu.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        UserService userService=new UserServiceImpl();
        List<User> users = userService.selectLike(username);
        List<User> list = userService.findAll();
        String json1 = new ObjectMapper().writeValueAsString(list);
        System.out.println(json1);
        String json = new ObjectMapper().writeValueAsString(users);

        resp.getWriter().write(json);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
